package tech.lq0.dreamaticvoyage.block.basic.misc;

import net.minecraft.core.BlockPos;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.MapColor;
import net.minecraft.world.level.material.PushReaction;
import tech.lq0.dreamaticvoyage.init.DamageSourceRegistry;
import tech.lq0.dreamaticvoyage.init.FluidRegistry;
import tech.lq0.dreamaticvoyage.init.ItemRegistry;

public class FukamizuPlumAcidBlock extends LiquidBlock {

    public FukamizuPlumAcidBlock() {
        super(FluidRegistry.FUKAMIZU_PLUM_ACID, Properties.of().mapColor(MapColor.WATER).strength(100.0f)
                .noCollission().noLootTable().liquid().pushReaction(PushReaction.DESTROY).replaceable().sound(SoundType.EMPTY));
    }

    @SuppressWarnings("deprecation")
    @Override
    public void entityInside(BlockState pState, Level pLevel, BlockPos pPos, Entity pEntity) {
        if (!pLevel.isClientSide) {
            if (pEntity instanceof ItemEntity itemEntity) {
                int tick = itemEntity.getPersistentData().getInt("FukamizuPlumAcidTick");
                if (!itemEntity.fireImmune()) {
                    if (tick >= 20) {
                        pLevel.playSound(null, pPos, SoundEvents.GENERIC_BURN, SoundSource.BLOCKS, 0.4F, 2.0F + pLevel.random.nextFloat() * 0.4F);
                        itemEntity.discard();
                    }
                } else {
                    if (tick >= 200) {
                        if (itemEntity.getItem().is(ItemRegistry.STURDY_BREAD_PLATE.get())) {
                            if (pLevel.random.nextDouble() > .1) {
                                ItemEntity itemEntity2 = new ItemEntity(pLevel, itemEntity.getX(), itemEntity.getY(), itemEntity.getZ(),
                                        new ItemStack(ItemRegistry.CHISELED_RUSK.get()));
                                pLevel.addFreshEntity(itemEntity2);
                            }
                        }
                        pLevel.playSound(null, pPos, SoundEvents.GENERIC_BURN, SoundSource.BLOCKS, 0.4F, 2.0F + pLevel.random.nextFloat() * 0.4F);
                        itemEntity.discard();
                    }
                }
                itemEntity.getPersistentData().putInt("FukamizuPlumAcidTick", tick + 1);
            } else {
                pEntity.hurt(DamageSourceRegistry.causeFukamizuPlumAcidDamage(pLevel.registryAccess(), null), 1f);
            }
        }
        super.entityInside(pState, pLevel, pPos, pEntity);
    }
}
